/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package buzzerproxy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; /** * CacheManager.java - beinhaltet alle Funktione zum Cachen eines Suchbegriffs und seiner Results * @author Sebastian Enger * @see Serialization * @since 1.1 * @version 1.1 / 2011-08-21 * @typ Class * */ public class CacheManager implements Serializable { private HashMap> cache = new HashMap>(); private ArrayList urlsOfSearchQuery = new ArrayList(); public CacheManager() { } public HashMap> getCache() { return cache; } public ArrayList getCacheArrayList() { return urlsOfSearchQuery; } public void setCacheItem(CacheItem it) { if (it != null) { urlsOfSearchQuery.add(it); } } // public void setCacheItem( CacheItem it ){ public void setCache(String searchquery, ArrayList a) { if (searchquery != null && a != null) { cache.put(searchquery, a); } } // public void setCache(String searchquery, ArrayList a ){ public boolean contains(String searchquery) { boolean hit = false; if (searchquery != null) { if (cache.containsKey(searchquery)) { hit = true; } } // if ( searchquery != null ){ return hit; } // public boolean contains(String searchquery){ public ArrayList getRangedCache(int start, int end, String filetyp, String searchquery) { ArrayList c = new ArrayList(); // return list ArrayList value = new ArrayList(); // temp content container list // long epoch = System.currentTimeMillis() / 1000; // int plus = 0; if (searchquery != null && start >= 0 && end > 0) { for (String key : cache.keySet()) { if (key.equalsIgnoreCase(searchquery)) { value = cache.get(key); if (start < value.size() && end < value.size()) { // System.out.println("Bin im Cache Bereich"); for (int i = start; i < end; i++) { // for (int i = start; i < end + plus; i++) { CacheItem it = value.get(i); c.add(it); // if ( it.getStoretime() + Constant.CACHE_VALID_THROUGH < epoch ){ // valider Cache Hit mit validem Zeitstempel // c.add(it); // } else { // value.remove(i); // plus = 1; // } // if ( it.getStoretime() + Constant.CACHE_VALID_THROUGH < epoch ){ } // for (int i = start; i= 0 && end >= 0) { return c; } // public class CacheManager { /** * CacheDeleter - beinhaltet alle Funktione zum Cachen eines Suchbegriffs und seiner Results * @author Sebastian Enger * @see Serialization * @since 1.1 * @version 1.1 / 2011-08-21 * @typ Class * */ static class CacheDeleter extends Thread { HashMap> cache = new HashMap>(); public CacheDeleter(HashMap> cache) { this.cache = cache; } public void discardCache() { ArrayList value = new ArrayList(); long epoch = System.currentTimeMillis() / 1000; int count = 0; for (String key : cache.keySet()) { value = cache.get(key); CacheItem it = value.get(count); long storetime = it.getStoretime(); if (storetime + Constant.CACHE_VALID_THROUGH > epoch) { // valider Cache Hit mit validem Zeitstempel value.remove(count); } // if ( it.getStoretime() + Constant.CACHE_VALID_THROUGH < epoch ){ count++; } // for (String key : cache.keySet()) { } // public void discardCache(){ @Override public void run() { discardCache(); } } // static class CacheDeleter extends Thread { static class Serialization { public Serialization() { } public void writeCache(String filename, CacheManager r) { if (filename != null) { try { //use buffering OutputStream file = new FileOutputStream(filename); OutputStream buffer = new BufferedOutputStream(file); ObjectOutput output = new ObjectOutputStream(buffer); try { output.writeObject(r); } finally { output.close(); } } catch (IOException ex) { Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, "CacheWrite Performance Failed", ex); } } // if ( filename != null ){ } // public void writeCache(String filename, HashMap> r) { public CacheManager readCache(String filename) { File f = new File(filename); CacheManager c = new CacheManager(); if (filename != null && f.exists() && f.isFile()) { try { //use buffering InputStream file = new FileInputStream(filename); InputStream buffer = new BufferedInputStream(file); ObjectInput input = new ObjectInputStream(buffer); try { //deserialize the List c = (CacheManager) input.readObject(); //display its data } finally { input.close(); } } catch (ClassNotFoundException ex) { // Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, "CacheRead Performance Failed", ex); } catch (IOException ex) { // Logger.getLogger(Serialization.class.getName()).log(Level.SEVERE, "CacheRead IO Error", ex); } } return c; } // public HashMap> readCache(String filename) { } // public class Serialization { static class CacheItem implements Serializable { private String filetyp = ""; private String url = ""; private long storetime; private String contentlength = ""; /** * @return the filename */ public String getFiletyp() { return filetyp; } /** * @param filename the filename to set */ public void setFilename(String filename) { this.setFiletyp(filename); } /** * @return the url */ public String getUrl() { return url; } /** * @param url the url to set */ public void setUrl(String url) { this.url = url; } /** * @return the storetime */ public long getStoretime() { return storetime; } /** * @param storetime the storetime to set */ public void setStoretime(long storetime) { this.storetime = storetime; } /** * @return the contentlength */ public String getContentlength() { return contentlength; } /** * @param contentlength the contentlength to set */ public void setContentlength(String contentlength) { this.contentlength = contentlength; } /** * @param filetyp the filetyp to set */ public void setFiletyp(String filetyp) { this.filetyp = filetyp; } } // class CacheItem { } // public class CacheManager {